package com.hserver.docker.filter;

import cn.hserver.core.ioc.annotation.Bean;
import cn.hserver.plugin.web.context.Webkit;
import cn.hserver.plugin.web.interfaces.FilterAdapter;
import cn.hserver.plugin.web.interfaces.HttpRequest;

import static com.hserver.docker.utils.FileUtil.readAuth;

/**
 * @author hxm
 */
@Bean
public class AuthFilter implements FilterAdapter {

    @Override
    public void doFilter(Webkit webkit) throws Exception {
        HttpRequest request = webkit.httpRequest;
        //webhook 回调放行
        if (request.getUri().contains("/deploy/deploy/")) {
            return;
        }
        String auth = request.getHeader("cookie");
        String s = readAuth();
        if (s != null) {
            if (auth == null || !auth.contains("auth=" + s)) {
                webkit.httpResponse.sendTemplate("/login.ftl");
            }
        }
    }
}
